﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Citi.RoutineBase;
using Citi.TasksAutomation.Domain.Sharepoint.OTFPC;
using Citi.Helpers;
using System.Globalization;
using System.Configuration;
using Citi.TasksAutomation.Domain;
using OfficeOpenXml;
using System.IO;
using System.Timers;
using Citi.SecurityHelper;


namespace Citi.TasksAutomation.SchedulerUI
{
    public class FridayTasks : IExecutableRoutine
    {
        private static Timer timer = new Timer { Interval = 120000 };
        static ICurrentAction CurrentAction;

        private static void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            //File.WriteAllText(Path.Combine(PtsReportTempPath, "PTSExecuting.txt"), string.Empty); 
            CurrentAction.Description = "Extracting";
        }

        public const string PathFolder = @"\\LACBRA001FS0010\DATA_GRP1\FINTOOLS\Config\Controls\TasksAutomation\";

        public void Rollback(ref ICurrentAction currentAction)
        {
            
        }

        public void Start(DateTime scheduledDate, ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            try
            {
                timer.Elapsed += TimerOnElapsed;
                CurrentAction = currentAction;
                timer.Start();

                var robotUsername = parameters["RobotUsername"].ToString();
                var robotPassword = parameters["RobotPassword"].ToString();

                using (ImpersonateCitiUser.GetImpersonateUser(robotUsername, robotUsername, robotPassword))
                {
                    var pendingTasks = new Task().GetAllTasks().
                        Where(x => x.TargetDate < DateTime.Today && x.Complete != 1
                            && x.StatusValue != "Cancelled"
                            && x.StatusValue != "Completed" && x.ActivityTypeValue != "VOE");

                    var task = new Domain.Task();

                    task.SendFollowUpManagerNotification(pendingTasks, ref currentAction);
                    task.SendFollowUpNotification(pendingTasks, ref currentAction);

                    var monthTasks = task.GetByMonth(DateTime.Today.Month, DateTime.Today.Year);

                    task.SendDashboardEmail(monthTasks, "BAU", ref currentAction);
                    //task.SendDashboardEmail(monthTasks, "VOE", ref currentAction);
                    task.GenerateExcelReport(monthTasks, ref currentAction);

                    task.SendEmails();
                }
            }
            finally
            {

                timer.Stop();
            }
        }

        public void Stop(ref ICurrentAction currentAction)
        {

        }
    }
}
